package com.youth.config.mq;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.support.converter.MessageConversionException;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Configuration;

/**
 * @ClassName RMQMessageConvert
 * @Description: TODO 自定义消息转换器,继承messageconverter接口
 * @Author yjwu
 * @Date 2020/1/6 14:32
 **/
@Slf4j
@Configuration
public class RMQMessageConvert implements MessageConverter {
    /**
     * @param messageProperties
     * @MethodName: toMessage
     * @Description: TODO 发送消息使用.
     * @Param: * @param o
     * @Return: {@link Message}
     * @Author: yjwu
     * @Date: 2020/1/6 14:35
     **/
    @Override
    public Message toMessage(Object o, MessageProperties messageProperties) throws MessageConversionException {
        String s = JSON.toJSONString(o);
        log.info("============RMQMessageConvert   toMessage ===========" + s);
        return new Message(s.getBytes(), messageProperties);
    }

    /**
     * @MethodName: fromMessage
     * @Description: TODO 接收消息
     * @Param: * @param message
     * @Return: {@link Object}
     * @Author: yjwu
     * @Date: 2020/1/6 14:36
     **/
    @Override
    public Object fromMessage(Message message) throws MessageConversionException {
        String content = new String(message.getBody());
        log.info("============RMQMessageConvert   fromMessage ===========" + content);
        return content;
    }
}
